/**
 * Orienteering Sport Diary Copyright (C) 2009 OSDiary Team
 *
 * This file is part of Orienteering Sport Diary (OSDiary).
 *
 * OSDiary is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see http://www.gnu.org/licenses/.
 */

package org.actions.event;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.sql.Timestamp;
import java.util.Map;
import org.data.Event;
import org.util.DateUtil;
import org.util.EventDAO;

/**
 *
 * @author Leonid Mytinski
 */
public class EditEventAction extends ActionSupport{
    private Integer eventId;
    private String name;
    private String place;
    private Timestamp timestamp;

    @Override
    public String execute(){
        Map session = ActionContext.getContext().getSession();
        Integer userId = (Integer) session.get("id");
        Event event = EventDAO.getUserEventById(userId, eventId);
        if (event != null){
            session.put("eventId", eventId);
            event.name = name;
            event.place = place;
            event.date = timestamp;
            if(EventDAO.updateEvent(event)) {
                return SUCCESS;
            }
        }
        return ERROR;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPlace(String place) {
        this.place = place;
    }

    public void setDateTimeString(String dateTimeString) {
        timestamp = new Timestamp(DateUtil.parse(dateTimeString, DateUtil.DATE_AND_TIME_DISPLAY_FORMAT));
    }

    public String getEventId() {
        return eventId.toString();
    }

    public void setEventId(String eventId) {
        this.eventId = Integer.parseInt(eventId);
    }
}
